package model.simulation;

import java.awt.Point;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.table.DefaultTableModel;
import model.Model;
import model.info.Constants;
import agents.DriverAgent;
import agents.PedestrianAgent;
import agents.intelligence.AgentAttributes;
import agents.intelligence.AgentParticipantInfo;
import agents.intelligence.IndividualProfile;
import aglobe.ontology.Libraries;

public class IndividualsBuilder {
	
	private Model model;
	private ArrayList<AgentAttributes> list;
	private Integer key;
	
	public IndividualsBuilder(Model model) {
		
		this.model = model;
		list = new ArrayList<AgentAttributes>();
		key = 1;
	}

	public ArrayList<AgentAttributes> getList() {
		return list;
	}

	public void setList(ArrayList<AgentAttributes> list) {
		this.list = list;
	}
	
	public Integer getKey() {
		return key;
	}

	public void setKey(Integer key) {
		this.key = key;
	}
	
	public void configureAgents(Integer type,Integer amount,Integer action) {
		
		AgentAttributes info = null;
		Integer result = null;
		Integer result2 = null;
		Point positions = null;
		switch(action) {
			case 0:
				if (list.isEmpty()) {
					info = new AgentAttributes(key,action,amount,type); 
					list.add(info);
					key = key+1;
				}
				else {
					Boolean found = false;
					int position = -1;
					for (int i=0;i<list.size() && !found;i++)
						if (list.get(i).getType().equals(type)) {
							found = true;
							position = i;
						}
					if (found) {	
						Integer participantes = amount + list.get(position).getAmount();
						list.get(position).setAmount(participantes);
					}
					else {
						info = new AgentAttributes(key,action,amount,type); 
						list.add(info);
						key = key+1;
					}
				}
				break;
			case 1:
				info = new AgentAttributes(key,action,amount,type);
				result = confirmPresence(info,1);
				result2 = confirmPresence(info,2);
				if (result < 0 && result2 < 0) {
					list.add(info);
					key = key+1;
				}
				else if (result >= 0 && result2 < 0) {
					model.updateWithoutContent(6);
				}
				else if (result < 0 && result2 >= 0) {
					list.add(info);
					key = key+1;
					positions = new Point(result2,list.size()-1);
					model.updateContent(7,positions);
				}
				break;
			case 2:
				info = new AgentAttributes(key,action,amount,type); 
				result = confirmPresence(info,1);
				result2 = confirmPresence(info,2);
				if (result < 0 && result2 < 0) {
					list.add(info);
					key = key+1;
				}
				else if (result < 0 && result2 >= 0) {
					Integer participantes = amount + list.get(result2).getAmount();
					list.get(result2).setAmount(participantes);
				}
				else if (result >= 0 && result2 < 0) {
					list.add(info);
					key = key+1;
					positions = new Point(result,list.size()-1);
					model.updateContent(5,positions);
				}	
				break;
		}
	}
	
	public void updateAgents(IndividualProfile newProfile,Integer position) {
		
		Integer result = null;
		Integer result2 = null;
		AgentAttributes info = list.get(position.intValue());
		IndividualProfile oldProfile = info.getProfile();
		info.setPerfil(newProfile);
		if (info.getSelection() == 1) {
			result = confirmPresence(info,1);
			result2 = confirmPresence(info,2);
			if (result >= 0 || result2 >= 0) {
				info.setPerfil(oldProfile);
				model.updateWithoutContent(10);
			}
		}
		else if (info.getSelection() == 2) {
			result = confirmPresence(info,1);
			if (result >= 0) {
				info.setPerfil(oldProfile);
				model.updateWithoutContent(10);
			}
		}
	}
	
	public Integer confirmPresence(AgentAttributes element,Integer type) {
		
		Boolean found = false;
		Integer result = -1;
		Integer i=0;
		while(i<list.size() && !found) {
			AgentAttributes test = list.get(i);
			if (test.getSelection().equals(type)) {
				if (element.getProfile().compareTo(test.getProfile()) == 1 && 
					element.getType() == test.getType()) {
					found = true;
					result = i;
				}
				else
					i=i+1;
			}
			else
				i=i+1;
		}
		return result;
	}

	public void eraseType(Integer position) {
		
		list.remove(position.intValue());
		updateTable();
	}
	
	public void cleanTypeList() {
		
		list.clear();
	}

	public void updateTable() {
		
		DefaultTableModel tableModel = new DefaultTableModel();
		tableModel.setColumnIdentifiers(new String[]{Constants.CLAVE,Constants.INDIVIDUOS,Constants.CONFIGURACION,Constants.CANTIDAD});
		String selection = Constants.BLANK;
		String participant = Constants.BLANK;
		String amount = Constants.BLANK;
		for (int i=0;i<list.size();i++) {
			AgentAttributes element = list.get(i);
			if (element.getSelection() == 0) {
				selection = Constants.ALEATORIOS;
				amount = element.getAmount().toString();
			}
			else if (element.getSelection() == 1) {
				selection = Constants.EXCLUIDOS;
				amount = Constants.BLANK;
			}
			else if (element.getSelection() == 2) {
				selection = Constants.INCLUIDOS;
				amount = element.getAmount().toString();
			}
			if (element.getType() == 0)
				participant = Constants.CONDUCTORES;
			else if (element.getType() == 1)
				participant = Constants.PEATONES;
			tableModel.addRow(new Object[]{element.getKey(),participant,selection,amount});
		}
		model.updateContent(8,tableModel);
	}
	
	public AgentAttributes consultElement(Integer position) {
		
		AgentAttributes element = list.get(position);
		return element;
	}
}
